package com.innovate.nms.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.google.common.eventbus.Subscribe;
import com.innovate.nms.domain.InspectHistory;
import com.ivan.toolkit.common.Events;

public class HistoryPanel extends JPanel implements TableModelListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8047733200171493140L;
	private JTable table;

	/**
	 * Create the panel.
	 */
	public HistoryPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setFont(new Font("新宋体", Font.PLAIN, 18));
		table.setRowHeight(32);
		scrollPane.setViewportView(table);
		TheTableModel model = new TheTableModel();
		table.setModel(model);
		model.addTableModelListener(this);
		
		int rowCount= table.getRowCount();
		table.getSelectionModel().setSelectionInterval(rowCount-1, rowCount-1);
		Rectangle rect=table.getCellRect(rowCount-1,0,true);
		table.updateUI();
		table.scrollRectToVisible(rect);
	}	
	
	@Override
	public void tableChanged(TableModelEvent e) {
		int rowCount= table.getRowCount();
		table.getSelectionModel().setSelectionInterval(rowCount-1, rowCount-1);
		Rectangle rect=table.getCellRect(rowCount-1,0,true);
		table.updateUI();
		table.scrollRectToVisible(rect);
	}

	public class TheTableModel extends AbstractTableModel {

		/**
		 * 
		 */
		private static final long serialVersionUID = -9212398416180701036L;

		public TheTableModel() {
			Events.register(this);
		}
		
		@Subscribe
		public void onEvent(Events event) {
			
			if(event.getKey() instanceof InspectHistory) {
				this.fireTableDataChanged();
			}
		}

		@Override
		public int getRowCount() {
			InspectHistory his = InspectHistory.instance.get();
			return his.getResults().size();
		}

		@Override
		public int getColumnCount() {
			return 8;
		}

		@Override
		public String getColumnName(int column) {
			switch(column) {
			case 0: return "开始时间";
			case 1: return "耗时（ms）";
			case 2: return "有线通道";
			case 3: return "无线通道";
			case 4: return "EMS->EMU1";
			case 5: return "EMU1->EMU2";
			case 6: return "EMU2->EMU3";
			case 7: return "EMU3->RTU";
			default: return "";
			}
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			
			try {
			
			InspectHistory his = InspectHistory.instance.get();
			InspectHistory.Entry entry = his.getResults().get(rowIndex);
			
				switch(columnIndex) {
				case 0: return dateFormat.format(entry.getTime());
				case 1: return entry.getCost();
				case 2: return entry.getValue().get("InspectChannelUdp");
				case 3: return entry.getValue().get("InspectChannelSms");
				case 4: return entry.getValue().get("InspectEmsEmu1");
				case 5: return entry.getValue().get("InspectEmu1Emu2");
				case 6: return entry.getValue().get("InspectEmu2Emu3");
				case 7: return entry.getValue().get("InspectEmu3Rtu");
				default: return "";
				}
			
			} catch(Exception e) {
				return "";
			}
			
		}
		
	}
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

}
